import Vue from "vue";
import {addCartData,getCartData,clearCartData,delCartData,setAmountData} from "../../api/cart";
export default{ 
    namespaced:true,
    state:{
        cartData:[],//购物车里的菜品数据
        cartCount:0,//购物车里菜品的数量
        isPack:false//是否自提
    },
    mutations:{
        //添加购物车数据
        ["ADD_CART_DATA"](state,payload){
            let isSame=false;//是否有相同的菜品
            state.cartCount=0;
            if(state.cartData.length>0){
                //从addCart传递过来的套餐里菜品明细的gid
                let curNewMealItems=(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):""
                for(let i=0;i<state.cartData.length;i++){
                    //购物车里的套餐菜品明细的gid
                    let newMealItems=(state.cartData[i].new_meal_items && state.cartData[i].new_meal_items.length>0)?JSON.stringify(state.cartData[i].new_meal_items):""
                    //如果有相同的菜品
                    if(state.cartData[i].gid==payload.cartData.gid && newMealItems==curNewMealItems){
                        isSame=true;
                        //增加购物车里菜品的数量
                        state.cartData[i].amount=parseInt(state.cartData[i].amount)+1;
                        //如果套餐里存在菜品
                        if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                            for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                state.cartData[i].meal_items[j].amount=state.cartData[i].amount*state.cartData[i].meal_items[j].dis_amount;
                            }
                        }
                    }
                    //计算购物车里菜品的数量
                    state.cartCount+=parseInt(state.cartData[i].amount);
                }
            }
            if(!isSame){
                state.cartData.unshift(payload.cartData);
                state.cartCount+=parseInt(payload.cartData.amount);
            }
        },
        //设置购物车里菜品的数据
        ["SET_CART_DATA"](state,payload){
            state.cartData=payload.cartData;
            state.cartCount=0;
            if(state.cartData.length>0){
                for(let i=0;i<state.cartData.length;i++){
                    state.cartCount+=parseInt(state.cartData[i].amount);
                }
            }
        },
        //清空购物车里的数据
        ["CLEAR_CART_DATA"](state){
            state.cartData=[];
            state.cartCount=0;
        },
        //设置自提
        ["SET_PACK"](state,payload){
            state.isPack=payload.isPack;
        },
        //将购物车里菜品的数量赋值给菜品展示
        ["SET_GOODS_CART_AMOUNT"](state,payload){
            if(state.cartData.length>0){
                for(let i=0;i<state.cartData.length;i++){
                    let curNewMealItems=(state.cartData[i].new_meal_items && state.cartData[i].new_meal_items.length>0)?JSON.stringify(state.cartData[i].new_meal_items):"";
                    for(let j=0;j<payload.goods.length;j++){
                        for(let k=0;k<payload.goods[j].goods.length;k++){
                            let newMealItems=(payload.goods[j].goods[k].new_meal_items && payload.goods[j].goods[k].new_meal_items.length>0)?JSON.stringify(payload.goods[j].goods[k].new_meal_items):"";
                            //如果有相同的菜品
                            if(state.cartData[i].gid==payload.goods[j].goods[k].gid && curNewMealItems==newMealItems){
                                payload.goods[j].goods[k].amount=state.cartData[i].amount;
                                break;
                            }
                        }
                    }
                }
            }else{//购物车里没有数据，将菜品展示里的数量设置为0
                if(payload.goods.length>0){
                    for(let i=0;i<payload.goods.length;i++){
                        for(let j=0;j<payload.goods[i].goods.length;j++){
                            payload.goods[i].goods[j].amount=0;
                        }
                    }
                }
            }
        },
        //减少和删除购物车里的数据
        ["DEL_CART_DATA"](state,payload){
            if(state.cartData.length>0){
                state.cartCount=0;
                let curNewMealItems=(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):"";
                for(let i=0;i<state.cartData.length;i++){
                    let newMealItems=(state.cartData[i].new_meal_items && state.cartData[i].new_meal_items.length>0)?JSON.stringify(state.cartData[i].new_meal_items):"";
                    //如果有相同的菜品
                    if(state.cartData[i].gid==payload.cartData.gid && curNewMealItems==newMealItems){
                        //减少菜品的数量
                        let cartAmount=parseInt(state.cartData[i].amount)>0?parseInt(state.cartData[i].amount)-1:0;
                        state.cartData[i].amount=cartAmount;
                        if(cartAmount<=0){//如果菜品的数量小于1
                            state.cartData.splice(i--,1);//删除菜品
                        }else{
                            if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                                for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                    //更改套餐里菜品的数量
                                    state.cartData[i].meal_items[j].amount=cartAmount*state.cartData[i].meal_items[j].dis_amount;
                                }
                            }
                        }
                        break;
                    }
                }
                for(let i=0;i<state.cartData.length;i++){
                    state.cartCount+=parseInt(state.cartData[i].amount);
                }
            }
        },
        //显示input数量输入框
        ["SHOW_AMOUNT_INPUT"](state,payload){
            state.cartData[payload.index].isAmountInput=true;
            state.cartData[payload.index].isAmountInputFocus=true;
            Vue.set(state.cartData,payload.index,state.cartData[payload.index]);
        },
        //隐藏input数量输入框
        ["HIDE_AMOUNT_INPUT"](state,payload){
            state.cartData[payload.index].isAmountInput=false;
            state.cartData[payload.index].isAmountInputFocus=false;
            Vue.set(state.cartData,payload.index,state.cartData[payload.index]);
        },
        //更新购物车数量
        ["SET_AMOUNT"](state,payload){
            state.cartCount=0;
            let amount=payload.cartData.amount;
            //如果输入的非数字，替换成空
            amount=amount.toString().replace(/[^\d]/g,"");
            //如果输入的数字为空或是0,强行设置为1
            if(!amount || amount=='0'){
                amount=1;
            }
            if(state.cartData.length>0){
                let curNewMealItems=(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):"";
                for(let i=0;i<state.cartData.length;i++){
                    let newMealItems=(state.cartData[i].new_meal_items && state.cartData[i].new_meal_items.length>0)?JSON.stringify(state.cartData[i].new_meal_items):"";
                    //如果有相同的菜品
                    if(state.cartData[i].gid==payload.cartData.gid && curNewMealItems==newMealItems){
                        //设置菜品的数量
                        state.cartData[i].amount=parseInt(amount);
                        //如果套餐里有菜品
                        if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                            for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                state.cartData[i].meal_items[j].amount=state.cartData[i].meal_items[j].dis_amount*parseInt(amount);
                            }
                        }
                    }
                    //更新购物车里菜品的总数量
                    state.cartCount+=parseInt(state.cartData[i].amount);
                }
            }
        }
    },
    actions:{   
        //添加购物车
        addCart(conText,payload){
            //给服务端接口购物车的数据
            let data={
                "uid":conText.rootState.user.uid,
                "token":conText.rootState.user.token,
                "platform":conText.rootState.system.platform,
                "branch_shop_id":payload.cartData.branch_shop_id,//分店的id
                "table_code":payload.cartData.table_code,//桌号
                "gid":payload.cartData.gid,//菜品和套餐的唯一标识
                "amount":payload.cartData.amount,//菜品的数量
                "is_meal":payload.cartData.is_meal,//是否套餐
                "meal_type":payload.cartData.meal_type,//套餐类型。0：普通套餐，1：自定义套餐
                "meal_items":(payload.cartData.meal_items && payload.cartData.meal_items.length>0)?JSON.stringify(payload.cartData.meal_items):"",//套餐里菜品的明细
                "new_meal_items":(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):""//套餐里菜品的gid
            };
            addCartData(data).then(res=>{
                if(res.code==200){
                    conText.commit("ADD_CART_DATA",{cartData:payload.cartData});
                    conText.commit("SET_GOODS_CART_AMOUNT",{goods:conText.rootState.goods.goods});
                    if(payload.success){
                        payload.success();
                    }
                }
            })
        },
        //显示购物车数据
        showCartData(conText,payload){
            getCartData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    for(let i=0;i<res.data.length;i++){
                        res.data[i].isAmountInput=false;//是否显示input输入框
                        res.data[i].isAmountInputFocus=false;//是否获取焦点
                    }
                    conText.commit("SET_CART_DATA",{cartData:res.data});
                }else{
                    conText.commit("SET_CART_DATA",{cartData:[]});
                }
            })
        },
        //清空购物车
        clearCart(conText,payload){
            clearCartData({uid:conText.rootState.user.uid,token:conText.rootState.user.token,platform:conText.rootState.system.platform,...payload}).then(res=>{
                if(res.code==200){
                    conText.commit("CLEAR_CART_DATA");
                    conText.commit("SET_GOODS_CART_AMOUNT",{goods:conText.rootState.goods.goods})
                }
            })
        },
        //减少和删除购物车里的菜品
        delCart(conText,payload){
            let amount=0;
            let mealType=payload.meal_type;
            if(mealType!=1){//普通套餐
                amount=payload.cartData.amount;
            }else{//自定义套餐
                let cartData=JSON.parse(JSON.stringify(conText.state.cartData));
                if(cartData.length>0){
                    let curNewMealItems=(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):"";
                    for(let i=0;i<cartData.length;i++){
                        let newMealItems=(cartData[i].new_meal_items && cartData[i].new_meal_items.length>0)?JSON.stringify(cartData[i].new_meal_items):"";
                        //如果有相同的菜品
                        if(cartData[i].gid==payload.cartData.gid && curNewMealItems==newMealItems){
                            cartData[i].amount=parseInt(cartData[i].amount)>0?parseInt(cartData[i].amount)-1:0;
                            amount=cartData[i].amount;
                            break;
                        }
                    }
                }
            }
            let data={
                uid:conText.rootState.user.uid,
                token:conText.rootState.user.token,
                platform:conText.rootState.system.platform,
                branch_shop_id:payload.branch_shop_id,
                amount:amount,
                meal_items:(payload.cartData.meal_items && payload.cartData.meal_items.length>0)?JSON.stringify(payload.cartData.meal_items):"",
                new_meal_items:(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):"",
                gid:payload.cartData.gid
            }
            if(amount>0){
                //修改购物车数量
                setAmountData(data).then(res=>{
                    if(res.code==200){
                        conText.commit("DEL_CART_DATA",{cartData:payload.cartData})
                    }
                })
            }else{
                //删除菜品
                delCartData(data).then(res=>{
                    if(res.code==200){
                        conText.commit("DEL_CART_DATA",{cartData:payload.cartData})
                    }
                })
            }
        },
        //更新购物车数量
        setAmount(conText,payload){
            let data={
                uid:conText.rootState.user.uid,
                token:conText.rootState.user.token,
                platform:conText.rootState.system.platform,
                branch_shop_id:payload.branch_shop_id,
                amount:payload.cartData.amount,
                meal_items:(payload.cartData.meal_items && payload.cartData.meal_items.length>0)?JSON.stringify(payload.cartData.meal_items):"",
                new_meal_items:(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):"",
                gid:payload.cartData.gid
            }
            setAmountData(data).then(res=>{
                if(res.code==200){
                    conText.commit("SET_AMOUNT",{cartData:payload.cartData})
                }
            })
        }
    },
    getters:{
        total(state){
            let total=0;//菜品和套餐的总价格
            let mealTotal=0; //套餐的总价格
            let footTotal=0; //单品的总价格
            let packMealTotal=0; //套餐包装费总价格
            let packFootTotal=0; //单品包装费总价格
            if(state.cartData.length>0){
                for(let i=0;i<state.cartData.length;i++){
                    if(state.cartData[i].is_meal=='0'){//非套餐
                        footTotal+=parseFloat(state.cartData[i].price)*parseInt(state.cartData[i].amount);
                        if(state.isPack){
                            //计算包装费总价
                            packFootTotal+=parseFloat(state.cartData[i].pack_price)*parseInt(state.cartData[i].amount);
                        }
                    }
                    if(state.cartData[i].is_meal=='1'){//套餐
                        if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                            for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                mealTotal+=parseFloat(state.cartData[i].meal_items[j].price)*parseInt(state.cartData[i].meal_items[j].amount);
                                if(state.isPack){
                                    packMealTotal+=parseFloat(state.cartData[i].meal_items[j].pack_price)*parseInt(state.cartData[i].meal_items[j].amount);
                                }
                            }
                        }
                    }
                }
                total=footTotal+mealTotal+packFootTotal+packMealTotal;
            }
            if(total>0){
                total=parseFloat(total.toFixed(2));
            }
            return total;
        },
        //包装费总价格
        packTotal(state){
            let packTotal=0;//包装费总价格
            let packMealTotal=0; //套餐包装费总价格
            let packFootTotal=0; //单品包装费总价格
            if(state.cartData.length>0){
                for(let i=0;i<state.cartData.length;i++){
                    if(state.cartData[i].is_meal=='0'){//非套餐
                        if(state.isPack){
                            //计算包装费总价
                            packFootTotal+=parseFloat(state.cartData[i].pack_price)*parseInt(state.cartData[i].amount);
                        }
                    }
                    if(state.cartData[i].is_meal=='1'){//套餐
                        if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                            for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                if(state.isPack){
                                    packMealTotal+=parseFloat(state.cartData[i].meal_items[j].pack_price)*parseInt(state.cartData[i].meal_items[j].amount);
                                }
                            }
                        }
                    }
                }
                packTotal=packFootTotal+packMealTotal;
            }
            if(packTotal>0){
                packTotal=parseFloat(packTotal.toFixed(2));
            }
            return packTotal;
        }
    }
}